/// <reference path="../Bootstrap/js/bootstrap.min.js" />
/// <reference path="../Knockout/knockout-3.3.0.js" />
/// <reference path="jquery-1.11.2.min.js" />
/// <reference path="../JQueryValidation/jquery.validate.min.js" />

var ValidationUtility = {    
    ApplyValidationRules: function () {
        var self = this;
        $("#aspnetForm").validate({
            rules: {
                ModelName: {
                    required: true
                },
                DevelopmentSource: {
                    required: true
                },
                ModelDescription: {
                    required: true
                },
                ProductionSystem: {
                    required: true
                },
                TargetSubmissionDate: {
                    required: true,
                    dateITA: true
                },
                ScopeSubmitted: {
                    required: true
                },
                BusinessUnit: {
                    required: true
                },
                IVGHead: {
                    required: true
                },
                ModelOwner: {
                    required: true
                },
                BusinessUnitExecutive: {
                    required: true
                },
                StreamLead: {
                    required: true
                },
                MRO: {
                    required: true
                },

                /* Validation Home Fields Start */

                LeadValidator: {
                    required: true
                },

                /* Validation Home Fields End */

                /* Key Findings Fields Start */

                FindingType: {
                    required: true
                },
                FindingDescription: {
                    required: true
                },
                TargetDate: {
                    dateITA: true
                },
                CompletionDate: {
                    dateITA: true
                },

                /* Key Findings Fields End */

                RiskAcceptanceStartDate: {
                    dateITA: true
                },
                RiskAcceptanceEndDate: {
                    dateITA: true
                },
                RiskAcceptanceClosingDate: {
                    dateITA: true
                },
                AsOfDate: {
                    required: true,
                    dateITA: true
                },
                
            },
            messages: {
                ModelName: {
                    required: "Model Name is required."
                },
                DevelopmentSource: {
                    required: "Development Source is required."
                },
                ModelDescription: {
                    required: "Model Description is required."
                },
                ProductionSystem: {
                    required: "Production System is required."
                },
                TargetSubmissionDate: {
                    required: "Target Submission Date is required.",
                    dateITA: "Please enter valid date in format mm/dd/yyyy."
                },
                ScopeSubmitted: {
                    required: "Scope Submitted is required."
                },
                BusinessUnit: {
                    required: "Business Unit is required."
                },
                IVGHead: {
                    required: "IVG Head is required."
                },
                ModelOwner: {
                    required: "Model Owner is required."
                },
                BusinessUnitExecutive: {
                    required: "Business Unit Executive is required."
                },
                StreamLead: {
                    required: "Stream Lead is required."
                },
                MRO: {
                    required: "MRO is required."
                },

                /* Validation Home Fields Start */

                LeadValidator: {
                    required: "Lead Validator is required."
                },

                /* Validation Home Fields End */

                /* Key Findings Fields Start */

                FindingType: {
                    required: "Finding Type is required."
                },
                FindingDescription: {
                    required: "Finding Description is required."
                },
                TargetDate: {
                    dateITA: "Please enter valid date in format mm/dd/yyyy."
                },
                CompletionDate: {
                    dateITA: "Please enter valid date in format mm/dd/yyyy."
                },

                /* Key Findings Fields End */

                RiskAcceptanceStartDate: {
                    dateITA: "Please enter valid date in format mm/dd/yyyy."
                },
                RiskAcceptanceEndDate: {
                    dateITA: "Please enter valid date in format mm/dd/yyyy."
                },
                RiskAcceptanceClosingDate: {
                    dateITA: "Please enter valid date in format mm/dd/yyyy."
                },
                AsOfDate: {
                    required: "As of date is required.",
                    dateITA: "Please enter valid date in format mm/dd/yyyy."
                },
                
            },
            errorPlacement: function (error, element) {
                if (element.is('select')) {
                    element.closest('.dropdownlist').append(error);
                } else if (element.is('.datepicker')) {
                    element.closest('.input-group').parent().append(error);
                }
                else {
                    error.insertAfter(element);
                }
            },
            submitHandler: function (form) {
                alert('form is valid');
                return false; // nothing submitted
            },
            onsubmit: false
        });


        // Search for controls marked with the causesValidation flag 
        //  that are contained anywhere within elements marked as 
        //  validationGroups, and wire their click event up.
        $('.validationGroup .causesValidation').click(Validate);

        // Select any input[type=text] elements within a validation group
        //  and attach keydown handlers to all of them.
        $('.validationGroup :text').keydown(function (evt) {
            // Only execute validation if the key pressed was enter.
            if (evt.keyCode == 13) {
                // Find and store the next input element that comes after the
                //  one in which the enter key was pressed.
                var $nextInput = $(this).nextAll(':input:first');

                // If the next input is a submit button, go into validation.
                //  Else, focus the next form element as if enter == tab.
                if ($nextInput.is(':submit')) {
                    Validate(evt);
                }
                else {
                    evt.preventDefault();
                    $nextInput.focus();
                }
            }
        });
    }
}

function Validate(evt) {
    // Ascend from the button or input element that triggered the 
    //  event until we find a container element flagged with 
    //  .validationGroup and store a reference to that element.
    var $group = $(this).parents('.validationGroup');

    var isValid = true;

    // Descending from that .validationGroup element, find any input
    //  elements within it, iterate over them, and run validation on 
    //  each of them.
    $group.find(':input').each(function (i, item) {
        if (!$(item).valid())
            isValid = false;
    });

    // If any fields failed validation, prevent the button's click 
    //  event from triggering form submission.
    if (!isValid) {
        evt.preventDefault();
        evt.stopImmediatePropagation();
    }
    return isValid;
}